/**
 * Copyright (c) 2010 Abbcc Corp.
 * No 225,Wen Yi RD, Hang Zhou, Zhe Jiang, China.
 * All rights reserved.
 *
 * "AdminPrivInterceptor.java is the copyrighted,
 * proprietary property of Abbcc Company and its
 * subsidiaries and affiliates which retain all right, title and interest
 * therein."
 * 
 * Revision History
 *
 * Date              Programmer                   Notes
 * ---------    ---------------------  --------------------------------------------
 * 2009-12-6           yixiugg                      initial
 **/

package com.abbcc.module.admin;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.abbcc.common.CommonConst;
import com.abbcc.models.TdUqx;
import com.abbcc.models.TdUser;
import com.abbcc.service.BaseService;
import com.abbcc.util.BeansFactory;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 **AdminPrivInterceptor.java
 **/
public class UserPrivInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		//String actionName = invocation.getAction().toString();
		//String methodName = invocation.getProxy().getConfig().getMethodName();
		String actionName = invocation.getProxy().getActionName();
		String namespace = invocation.getProxy().getNamespace();
		ActionContext actionContext = invocation.getInvocationContext();
		Map session = actionContext.getSession();
		TdUser auser = (TdUser)session.get(CommonConst.SESSIONUSER);
		
			List<TdUqx> privList = (List<TdUqx>) session
					.get("uqxList");
			if(privList == null || privList.size() == 0){
				BaseService baseService = (BaseService)BeansFactory.get("baseService");
				TdUqx tu = new TdUqx();
				tu.setUserId(auser.getUserId());
				privList = baseService.findByExample(tu);
				session.put("uqxList", privList);
			}
			if (privList == null || privList.size() == 0)
				return "nopriv";
			String name = namespace+"/"+actionName;
			for(TdUqx tu:privList){
				if(tu.getQxAction().equals(name)){
					invocation.invoke();
					break;
				}
			}
		return "nopriv";
	}

}
